<?php
class A{
	public function __construct( $pathBase, $pathTest, $template ){

		$this->pathBase	= $pathBase;
		$this->pathTest	= $pathTest;
		$this->request	= new Net_HTTP_Request_Receiver;
		if( 1 || $this->request->isAjax() ){
			switch( $this->request->get( 'action' ) ){
				case 'test':
					$path	= $this->request->get( 'path' );
					if( $path ){
						sleep(0);
						$cwd	= getCwd();
						chdir( $this->pathBase );
						$a = $status = "";
						$temp1	= tempnam( './', 'json' );
						$temp2	= tempnam( './', 'xml' );
						@exec( 'phpunit --log-json '.$temp1.' --log-junit '.$temp2.' '.$path, $a, $status );
						$json	= "[".str_replace( "}{", "},{", file_get_contents( $temp1 ) )."]";
						$xml	= file_get_contents( $temp2 );
						unlink( $temp1 );
						unlink( $temp2 );
//						print( $json );
//						var_dump( json_decode( $json ) );
						switch( $this->request->get( 'format' ) ){
							case 'json':
								$data	= json_decode( $json );
								xmp( ADT_JSON_Formater::format( $data ) );
								die;
//								$this->respond( $data );
								break;
							case 'xml':
								header( 'Content-Type: text/xml' );
								$this->respond( $xml );
								break;
							case 'text':
								$this->respond( trim( implode( "\n", $a ) ) );
								break;
							default:
								$this->respond( trim( implode( "<br/>", $a ) ) );
						}
						chdir( $cwd );
						$this->respond( array( 'error' => 'invalid action or format' ) );
					}
			}
		}
		else{}
		$this->respond( $this->render( $template ) );
	}

	protected function respond( $data ){
		if( is_string( $data ) )
			print( $data );
		else{
//			header( 'Content-Type: text/json' );
			echo json_encode( $data );
		}
		exit;
	}
	
	protected function renderFileTreeRecursively( $path ){

		$index	= new DirectoryIterator( $path );
		$list	= array(
			array(),
			array()
		);
		foreach( $index as $item ){
			$children	= '';
			$fileName	= $item->getFilename();
			$pathName	= $item->getPathname();
			$classes	= array();
			$attributes	= array( 'data-uri' => $pathName );
			if( $item->isDot() || $fileName == '.svn' )
				continue;
			if( !$item->isDir() && !preg_match( '/Test.php.?$/', $fileName ) )
				continue;
			if( $item->isDir() ){
				$type		= 0;
				$classes[]	= 'folder';
				$key		= str_replace( '/', '_', $pathName );
				$children	= $this->renderFileTreeRecursively( $pathName );
				$attributes['data-uri'] = $pathName;
			}
			else{
				$type		= 1;
				$classes[]	= 'file';
				$key		= str_replace( '/', '_', preg_replace( '/.php.?$/i', '', $pathName ) );
				$fileName	= preg_replace( '/Test.php.?$/i', '', $fileName );
				$attributes['data-uri'] = $key;
			}
			$attributes['id'] = 'node-'.$key;
//			if( $children )
			if( $classes )
				$attributes['class']	= join( ' ', $classes );
			$label	= UI_HTML_Tag::create( 'span', $fileName, array( 'class' => 'label' ) );
			$list[$type][$fileName]	= UI_HTML_Tag::create( 'li', $label.$children, $attributes );
		}
		ksort( $list[0] );
		ksort( $list[1] );
		if( !$list[0] && !$list[1] )
			return '';
		return UI_HTML_Tag::create( 'ul', join( $list[0] ) . join( $list[1] ) );
	}

	protected function render( $template ){
		$page	= new UI_HTML_PageFrame();
		$cwd	= getCwd();
		chdir( $this->pathBase );
		$data	= array(
			'tree'	=> $this->renderFileTreeRecursively( $this->pathTest ),
		);
		chdir( $cwd );
		$body	= UI_Template::renderString( $template, $data );
		$page->addJavaScript( 'http://js.ceusmedia.de/jquery/1.4.4.min.js' );
		$page->addJavaScript( 'http://js.ceusmedia.de/String.pad.js' );
		$page->addJavaScript( 'script.js' );
		$page->addStylesheet( 'http://css.ceusmedia.de/blueprint/reset.css' );
		$page->addStylesheet( 'http://css.ceusmedia.de/blueprint/typography.css' );
		$page->addStylesheet( 'style.css' );
		$page->addBody( $body );
		return $page->build();
	}
}
?>